#!/bin/sh

# if app not exist
if [ ! -f /bin/inadyn ]; then
    exit 0
fi

# wait to start web and run from goahead code
. /etc/scripts/web_wait.sh
web_wait

# get params
. /etc/scripts/global.sh

LOG="logger -t dyndns"

start() {
    get_param
    if  [ "$DDNS" != "" ] && [ "$DDNSProvider" != "" ] && [ "$DDNSProvider" != "none" ] && [ "$DDNSAccount" != "" ] && [ "$DDNSPassword" != "" ]; then
	$LOG "Starting DynDNS"
	if [ "$DDNSProvider" = "dyndns.org" ]; then
	    inadyn -u $DDNSAccount -p $DDNSPassword -a $DDNS --dyndns_system dyndns@$DDNSProvider --update_period_sec $update_period --background --syslog
	elif [ "$DDNSProvider" = "zoneedit.com" ] || [ "$DDNSProvider" = "no-ip.com" ]; then
	    inadyn -u $DDNSAccount -p $DDNSPassword -a $DDNS --dyndns_system default@$DDNSProvider --update_period_sec $update_period --background --syslog
	elif [ "$DDNSProvider" = "freedns.afraid.org" ]; then
	    inadyn -a $DDNS,$DDNSAccount --dyndns_system default@$DDNSProvider --update_period_sec $update_period --background --syslog
	else
	    $LOG "Unknown DDNS provider: $DDNSProvider"
	    exit 1
	fi
    fi
}

get_param() {
    eval `nvram_buf_get 2860 DDNS DDNSProvider DDNSAccount DDNSPassword`
    update_period="43200"
}

stop() {
  $LOG "Stopping DynDns"
    killall -q inadyn
    killall -q -SIGKILL inadyn
}

case "$1" in
        start)
            start
            ;;

        stop)
            stop
            ;;

        restart)
            stop
            start
            ;;

        *)
            echo $"Usage: $0 {start|stop|restart}"
            exit 1
esac
